import json

# 打开你的json文件
with open("demo.json", "r", encoding="utf-8") as f:
    data = json.load(f)

# 准备三个类别的数据
limit_up_numbers = []  # 涨停数字
limit_up_reasons = []  # 涨停理由
plate_names = set()  # 板块名称集合（用于去重）

for item in data["data"]:
    # 处理股代码格式转换
    raw_symbol = item["symbol"]
    if raw_symbol.endswith(".SZ"):
        symbol = "SZ" + raw_symbol.split('.')[0]
    elif raw_symbol.endswith(".SS"):
        symbol = "SH" + raw_symbol.split('.')[0]
    else:
        symbol = raw_symbol

    # 涨停数字：股代码 + 板块排序
    board_rank = item.get("m_days_n_boards_boards", 0)
    limit_up_numbers.append((symbol, board_rank))

    # 涨停理由：股代码 + 板块名称
    surge_reason = item.get("surge_reason", {})
    related_plates = surge_reason.get("related_plates", [])

    # 提取所有相关板块名称
    for plate in related_plates:
        plate_name = plate.get("plate_name", "")
        if plate_name:
            limit_up_reasons.append((symbol, plate_name))
            plate_names.add(plate_name)  # 添加到板块名称集合

# 创建板块索引（按字母顺序排序）
plate_index = sorted(list(plate_names))

# 输出涨停数字
print("涨停数字")
print("股代码\t板块排序")
for symbol, rank in limit_up_numbers:
    print(f"{symbol}\t{rank}")

print("\n涨停理由")
print("股代码\t板块名称")
for symbol, reason in limit_up_reasons:
    print(f"{symbol}\t{reason}")

print("\n板块索引")
for i, plate_name in enumerate(plate_index, 1):
    print(f"{i}\t{plate_name}")